<?php
namespace app\admin\model;

use think\Model;

class Lesson extends Model{
    /**
     * 获取所有记录信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){
        $map='';
        $order="";
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['user.full_name|user.tel|user.username|le.lesson_sn' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $lesson = $this
            ->alias('le')
            ->join('subject s','le.subject_id = s.id','LEFT')
            ->join('lesson_program p','le.program_id = p.id','LEFT')
            ->join('user user','le.teacher_id = user.id','LEFT')
            ->join('classroom cla','le.classroom_id = cla.id','LEFT')
            ->join('service_target tar','le.service_target_id = tar.id','LEFT')
            ->join('class css','le.class_id = css.id','LEFT')
            ->join('lesson_nature na','le.lesson_nature_id = na.id','LEFT')
            ->join('start_lesson sle','le.start_lesson_id = sle.id','LEFT')
            ->where($where)
            ->where($map)
            ->order($order)
            ->field('le.id,le.teacher_id,le.cost_type,le.com_lesson_num,le.registration_end_time,
                    le.lesson_name,le.lesson_sn,le.lesson_cost,
                    le.lesson_num,le.is_approve,user.full_name as teacher_name,
                    le.grade_id')
            ->select();
        if($lesson){
            foreach($lesson as $key =>$val){
                if(!empty($lesson[$key]['teacher_group_images'])){
                    $lesson[$key]['teacher_group_images'] = json_decode(htmlspecialchars_decode($lesson[$key]['teacher_group_images']));
                }
                $lesson[$key] = $val->toArray();
            }
        }
        return $lesson;
    }

    /**
     * 获取所有记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount(){
        $map='';
        $order="";
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['user.full_name|user.tel|user.username|le.lesson_sn' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $count = $this
            ->alias('le')
            ->join('subject s','le.subject_id = s.id','LEFT')
            ->join('lesson_program p','le.program_id = p.id','LEFT')
            ->join('user user','le.teacher_id = user.id','LEFT')
            ->join('classroom cla','le.classroom_id = cla.id','LEFT')
            ->join('service_target tar','le.service_target_id = tar.id','LEFT')
            ->join('class css','le.class_id = css.id','LEFT')
            ->join('lesson_nature na','le.lesson_nature_id = na.id','LEFT')
            ->join('start_lesson sle','le.start_lesson_id = sle.id','LEFT')
            ->where($where)
            ->where($map)
            ->order($order)
            ->field('le.id,le.teacher_id,le.cost_type,le.com_lesson_num,le.registration_end_time,
                    le.lesson_name,le.lesson_sn,le.lesson_cost,
                    le.lesson_num,le.is_approve,user.full_name as teacher_name,
                    le.grade_id')
            ->count();
        return $count;
    }

    /**
     * 通过Id获取单条信息详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getLessonById($id){
        $info = $this
            ->alias('le')
            ->join('city c','le.city_id = c.id','LEFT')
            ->join('subject s','le.subject_id = s.id','LEFT')
            ->join('lesson_program p','le.program_id = p.id','LEFT')
            ->join('user user','le.teacher_id = user.id','LEFT')
            ->join('classroom cla','le.classroom_id = cla.id','LEFT')
            ->join('service_target tar','le.service_target_id = tar.id','LEFT')
            ->join('class css','le.class_id = css.id','LEFT')
            ->join('lesson_nature na','le.lesson_nature_id = na.id','LEFT')
            ->join('start_lesson sle','le.start_lesson_id = sle.id','LEFT')
            ->join('user education_user','le.education_user = education_user.id','LEFT')
            ->join('user cooperate_user','le.cooperate_user = cooperate_user.id','LEFT')
            ->join('user material_user','le.material_user = material_user.id','LEFT')
            ->join('user special_user1','le.special_user1 = special_user1.id','LEFT')
            ->join('user special_user2','le.special_user2 = special_user2.id','LEFT')
            ->join('user special_user3','le.special_user3 = special_user3.id','LEFT')
            ->join('user special_user4','le.special_user4 = special_user4.id','LEFT')
            ->join('user special_user5','le.special_user5 = special_user5.id','LEFT')
            ->where('le.id='.$id)
            ->field('le.*,c.name,s.subject,p.program_full_name,p.program,
                    tar.service_target,css.class_type,na.lesson_nature,lesson_start_time,
                    user.full_name as teacher_name,cla.classroom_address
                    ,education_user.full_name as education_user
                    ,cooperate_user.full_name as cooperate_user
                    ,material_user.full_name as material_user
                    ,special_user1.full_name as special_user1
                    ,special_user2.full_name as special_user2
                    ,special_user3.full_name as special_user3
                    ,special_user4.full_name as special_user4
                    ,special_user5.full_name as special_user5
                    ')
            ->find();
        if($info){
            $info = $info->toArray();
            if(!empty($info['teacher_group_images'])){
                $info['teacher_group_images'] = json_decode(htmlspecialchars_decode($info['teacher_group_images']));
            }
        }
        return $info;
    }

    /**
     * 获取结算信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getLessonSettlement($id){
        $where = 'lesson.id='.$id;
        $lesson_settlement = $this
            ->alias('lesson')
            ->join('user education','lesson.education_user = education.id','LEFT')
            ->join('user cooperate','lesson.cooperate_user = cooperate.id','LEFT')
            ->join('user material','lesson.material_user = material.id','LEFT')
            ->field('lesson.id,lesson.is_reclass,lesson.lesson_name,lesson.lesson_cost,lesson.lesson_num,lesson.is_teacher_cost,lesson.teacher_cost_class,lesson.teacher_cost_person,lesson.teacher_bdcost,
 lesson.is_classroom_cost,lesson.classroom_cost_class,lesson.classroom_cost_person,lesson.classroom_bdcost,lesson.is_education_cost,lesson.education_cost_class,lesson.education_cost_person,lesson.education_bdcost,lesson.reclass_cost,lesson.rec_student_cost,lesson.rec_teacher_cost,lesson.rec_classroom_cost,lesson.cooperate_cost,lesson.material_cost,lesson.total_rec_cost,lesson.province_rec_cost,lesson.city_rec_cost,lesson.area_rec_cost,lesson.school_rec_cost,lesson.special_user1_cost,lesson.special_user2_cost,lesson.special_user3_cost,lesson.special_user4_cost,lesson.special_user5_cost,lesson.classroom_cost_type,lesson.teacher_cost_type,lesson.education_cost_type,lesson.classroom_user_cost,lesson.education_user,lesson.material_user,lesson.special_user1,lesson.special_user2,lesson.special_user3,lesson.special_user4,lesson.special_user5,lesson.cooperate_user,education.user_auth as edu_auth,cooperate.user_auth as coo_auth')
            ->where($where)
            ->find()
            ->toArray();
        return $lesson_settlement;
    }
}